package com.polymer.app.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


import com.polymer.app.annotation.SensitiveInfo;


/**
 * 
     * 工具类，获取脱敏后的VO对象字符串
     * 脱敏打印专用
 *
 */
public final class Json {

    /**
     * 获取脱敏json串 
     * 
     * @param javaBean
     * @return
     */
    public static String toString(Object javaBean) {
        String json = "";
        if (null != javaBean) {
            Class<? extends Object> raw = javaBean.getClass();
            try {
                if (raw.isInterface()) return json;

                json = Json.replace(javaBean);
            } catch (Exception e) {

            }
        }
        return json;
    }

    private static Field[] findAllField(Class<?> clazz) {
        Field[] fileds = clazz.getDeclaredFields();
        while (null != clazz.getSuperclass() && !Object.class.equals(clazz.getSuperclass())) {
            fileds = (Field[]) ArraysUtils.addArrays(fileds, clazz.getSuperclass().getDeclaredFields());
            clazz = clazz.getSuperclass();
        }
        return fileds;
    }

    private static String replace(Object javaBean) throws IllegalArgumentException, IllegalAccessException {
        Class<? extends Object> raw = javaBean.getClass();
        Field[] fields = Json.findAllField(raw);
        StringBuilder sb = new StringBuilder("{");
        if (null != fields && fields.length > 0) {
            for (Field field : fields) {
                field.setAccessible(true);
                if (null != field && null != javaBean) {
                    Object value = field.get(javaBean);

                    // 处理自身的属性
                    SensitiveInfo annotation = field.getAnnotation(SensitiveInfo.class);
                    if ("serialVersionUID".equals(field.getName())) {
                        continue;
                    }
                    if (field.getType().equals(String.class) && null != annotation) {
                        String valueStr = (String) value;
                        if (StringUtil.isNotBlank(valueStr)) {
                            switch (annotation.type()) {
                                case MOBILE: {
                                    appendValue(sb, field, Desensitization.desing(valueStr, 1));
                                    break;
                                }
                                case REAL_NAME: {
                                    appendValue(sb, field, Desensitization.desing(valueStr, 2));
                                    break;
                                }
                                case BANK_CARD_NO: {
                                    appendValue(sb, field, Desensitization.desing(valueStr, 3));
                                    break;
                                }
                                case USER_CARD_NO: {
                                    appendValue(sb, field, Desensitization.desing(valueStr, 4));
                                    break;
                                }
                                default:
                                    appendValue(sb, field, valueStr);
                            }
                        }
                    } else {
                        if (null != value) {
                            sb.append(field.getName()).append(":").append(value).append(",");
                        }
                    }
                }
            }
        }
        return sb.append("}").toString();
    }

    private static void appendValue(StringBuilder sb, Field field, String valueStr) {
        sb.append(field.getName()).append(":").append(valueStr).append(",");
    }

    public static Method[] findAllMethod(Class<?> clazz) {
        Method[] methods = clazz.getMethods();
        return methods;
    }

    public static enum SensitiveType {
        /**
         * 姓名
         */
        REAL_NAME,

        /**
         * 身份证号
         */
        USER_CARD_NO,

        /**
         * 银行卡号
         */
        BANK_CARD_NO,

        /**
         * 手机号
         */
        MOBILE;
    }

}